package com.efast.cafe.portal.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiRequestUriBean;
import com.efast.cafe.portal.bean.api.ApiRequestUriQueryBean;
import com.efast.cafe.portal.entity.api.ApiModuleRequriDef;
import com.efast.cafe.portal.entity.api.ApiSystemModuleDef;
import com.efast.cafe.portal.service.api.IApiRequestUriService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/frame_center/api")
public class ApiRequestUriController extends AbstractController{
	
	@Autowired
	private IApiRequestUriService apiRequestUriService;
	
	
	/**
	 * 检验uri信息的有效性
	 * @param apiRequestUri
	 */
	private void checkUriInfoBean( ApiRequestUriBean apiRequestUri){
		if("".equals(apiRequestUri.getPathName())||apiRequestUri.getPathName()==null){
			throw new ServiceException("msg.portal.api.uri.name.empty");
		}
		if("".equals(apiRequestUri.getPathUri())||apiRequestUri.getPathUri()==null){
			throw new ServiceException("msg.portal.api.uri.uri.empty");
		}
		if(!apiRequestUri.getPathUri().startsWith("/")){
			throw new ServiceException("msg.portal.api.uri.uri.error");
		}
	
	}
	/**
	 * 页面跳转
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/uri/page")
	public String templatePage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response){
		
		return "/api/uri/list";
	}

	/**
	 * 查询 某个module uri分页
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/uri/list")
	public @ResponseBody DataTableVo<ApiRequestUriBean> queryPageList(@ModelAttribute ApiRequestUriQueryBean queryBean, 
			HttpServletRequest request, HttpServletResponse response){
		return outPageJSONArray_bootstrapDatatable(apiRequestUriService.queryPageList(queryBean));
	}
	
	/**
	 * 跳转详细页面
	 * @param uriId
	 * @param moduleVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
//	@RequestMapping(value = "/uri/detail")
	public String detail(@RequestParam(required = true) Integer systemId,@RequestParam(required = true) Integer moduleId,@RequestParam(required = true) Integer uriId, 
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		ApiRequestUriBean apiRequestUriBean = null;
		apiRequestUriBean= apiRequestUriService.getUriDetail(systemId,moduleId,uriId);
		JSONObject ajaxsJson = JSONObject.fromObject(apiRequestUriBean);
		model.addAttribute("ajaxsJson", ajaxsJson);
		return "/api/uri/detail";
	}
	
	/**
	 * 跳转编辑或者新增页面
	 * @param uriId
	 * @param moduleVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
//	@RequestMapping(value = "/uri/form")
	public String formPage(@ModelAttribute ApiRequestUriBean apiRequestUriBean,
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		if(apiRequestUriBean.getId()!=null){
			apiRequestUriBean= apiRequestUriService.getUriDetail(apiRequestUriBean.getSystemId(),apiRequestUriBean.getModuleId(),apiRequestUriBean.getId());
		}
		JSONObject ajaxsJson = JSONObject.fromObject(apiRequestUriBean);
		model.addAttribute("ajaxsJson", ajaxsJson);
		return "/api/uri/form";
	}
	
	/**
	 * 跳转编辑或者新增页面
	 * @param uriId
	 * @param moduleVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
//	@RequestMapping(value = "/uri/form2")
	public String formPageTwo(@ModelAttribute ApiRequestUriBean apiRequestUriBean, 
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		if(apiRequestUriBean.getId()!=null){
			apiRequestUriBean=apiRequestUriService.getUriDetail(apiRequestUriBean.getSystemId(),apiRequestUriBean.getModuleId(),apiRequestUriBean.getId());
		}
		JSONObject ajaxsJson = JSONObject.fromObject(apiRequestUriBean);
		model.addAttribute("ajaxsJson", ajaxsJson);
		return "/api/uri/form2";
	}


	/**
	 * 通过url查询uri详细信息
	 * @param url
	 * @param systemVersion
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/uri/searchbyurl")
	public @ResponseBody  ApiRequestUriBean	searchByUrl(@ModelAttribute ApiRequestUriBean apiRequestUriBean, @RequestParam(required = false) String systemVersion, 
			HttpServletRequest request, HttpServletResponse response){
		return apiRequestUriService.searchByUrl(apiRequestUriBean);
	}
	

	
	/**
	 * 新增个uri详细信息
	 * @param uriId
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/uri/add")
	public @ResponseBody ResultBean uriAdd(@ModelAttribute ApiRequestUriBean apiRequestUri,
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try {
			checkUriInfoBean(apiRequestUri);
			id = apiRequestUriService.saveUri(apiRequestUri);
			result.setObject(id.toString());
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}

		return result;
	}
	
	/**
	 * 编辑个uri详细信息
	 * @param uriId
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/uri/edit")
	public @ResponseBody ResultBean uriEdit(@ModelAttribute ApiRequestUriBean apiRequestUri, 
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(EDIT_SUCCESS_TIP));
		try {
			 checkUriInfoBean(apiRequestUri);
			 apiRequestUriService.editUri(apiRequestUri);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
			}
		}
		return result;
	}
	
	/**
	 * 删除uri
	 * @param uriId
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/uri/del")
	public @ResponseBody ResultBean uriDel(@RequestParam(required = false) Integer uriId, 
			HttpServletRequest request, HttpServletResponse response){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(DEL_SUCCESS_TIP));
		try {
			apiRequestUriService.deleteUri(uriId);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}	
	
	/**
	 * 
	 * uri复用
	 * @param apiModuleRequriDef
	 * @param request
	 * @return
	 */
//	@RequestMapping(value = "/uri/reuse")
	public @ResponseBody ResultBean reuseApiUridefine(
			@ModelAttribute ApiRequestUriBean apiRequestUriBean,HttpServletRequest request){
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(""));
		try {
			apiRequestUriService.reuseApiUridefine(apiRequestUriBean);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(""));
			}
		}
		return result;
	}
	
}
